/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.CalendarEntity;
import entity.CompletedGoodsEntity;
import entity.DataObjectEntity;
import entity.ExpectedYearOnYearGrowthEntity;
import entity.MasterProductionScheduleMonthlyEntity;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Root;
import type.Month;
import type.ProductionType;
import type.SalesForecastPeriodType;

/**
 *
 * @author wei
 */
@Stateless
public class MasterProductionScheduleMonthlyEntityFacade extends AbstractFacade<MasterProductionScheduleMonthlyEntity> {

    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public MasterProductionScheduleMonthlyEntityFacade() {
        super(MasterProductionScheduleMonthlyEntity.class);
    }

    public ArrayList<CalendarEntity> getThisMonthPublicHoliday(Month month, int year) {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<CalendarEntity> calendarRoot = cq.from(CalendarEntity.class);
        cq.select(calendarRoot);
        cq.where(builder.and(builder.equal(calendarRoot.get("archived"), false), builder.equal(calendarRoot.get("month"), month), builder.equal(calendarRoot.get("year"), year)));

        ArrayList cal = new ArrayList<CalendarEntity>();
        for (Object o : em.createQuery(cq).getResultList()) {
            CalendarEntity c = (CalendarEntity) o;
            cal.add(c);
        }
        return cal;
    }

    public CompletedGoodsEntity getClassic() {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<CompletedGoodsEntity> completedGoodsRoot = cq.from(CompletedGoodsEntity.class);
        cq.select(completedGoodsRoot);
        cq.where(builder.and(builder.equal(completedGoodsRoot.get("archived"), false), builder.equal(completedGoodsRoot.get("productName"), "Classic Nutri Bar (Nuts)")));
        return (CompletedGoodsEntity) em.createQuery(cq).getSingleResult();
    }

    public CompletedGoodsEntity getPrem1ium() {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<CompletedGoodsEntity> completedGoodsRoot = cq.from(CompletedGoodsEntity.class);
        cq.select(completedGoodsRoot);
        cq.where(builder.and(builder.equal(completedGoodsRoot.get("archived"), false), builder.equal(completedGoodsRoot.get("productName"), "Premium Power Bar (Chocolate Nuts)")));
        return (CompletedGoodsEntity) em.createQuery(cq).getSingleResult();
    }

    public ArrayList<CompletedGoodsEntity> getAllClassic() {
        ArrayList classicbars = new ArrayList<CompletedGoodsEntity>();
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<CompletedGoodsEntity> completedGoodsRoot = cq.from(CompletedGoodsEntity.class);
        cq.select(completedGoodsRoot);
        cq.where(builder.and(builder.equal(completedGoodsRoot.get("archived"), false), builder.equal(completedGoodsRoot.get("productName"), "Classic Nutri Bar (Nuts)")));

        classicbars.add((CompletedGoodsEntity) em.createQuery(cq).getSingleResult());

        CriteriaBuilder builder1 = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq1 = builder1.createQuery();
        Root<CompletedGoodsEntity> completedGoodsRoot1 = cq1.from(CompletedGoodsEntity.class);
        cq1.select(completedGoodsRoot1);
        cq1.where(builder1.and(builder1.equal(completedGoodsRoot1.get("archived"), false), builder1.equal(completedGoodsRoot1.get("productName"), "Classic Nutri Bar (Fruits)")));

        classicbars.add((CompletedGoodsEntity) em.createQuery(cq1).getSingleResult());

        return classicbars;
    }

    public ArrayList<CompletedGoodsEntity> getAllPremium() {
        ArrayList premiumbars = new ArrayList<CompletedGoodsEntity>();
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<CompletedGoodsEntity> completedGoodsRoot = cq.from(CompletedGoodsEntity.class);
        cq.select(completedGoodsRoot);
        cq.where(builder.and(builder.equal(completedGoodsRoot.get("archived"), false), builder.equal(completedGoodsRoot.get("productName"), "Premium Power Bar (Chocolate Nuts)")));

        premiumbars.add((CompletedGoodsEntity) em.createQuery(cq).getSingleResult());

        CriteriaBuilder builder1 = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq1 = builder1.createQuery();
        Root<CompletedGoodsEntity> completedGoodsRoot1 = cq1.from(CompletedGoodsEntity.class);
        cq1.select(completedGoodsRoot1);
        cq1.where(builder1.and(builder1.equal(completedGoodsRoot1.get("archived"), false), builder1.equal(completedGoodsRoot1.get("productName"), "Premium Power Bar (Redcurrant Fruits)")));

        premiumbars.add((CompletedGoodsEntity) em.createQuery(cq1).getSingleResult());

        return premiumbars;
    }

    public DataModel listCreateMPS(SalesForecastPeriodType period, int year, ProductionType type) {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<MasterProductionScheduleMonthlyEntity> mpsRoot = cq.from(MasterProductionScheduleMonthlyEntity.class);
        cq.select(mpsRoot);
        cq.where(builder.and(builder.equal(mpsRoot.get("archived"), false), builder.equal(mpsRoot.get("type"), period), builder.equal(mpsRoot.get("yearOfProduction"), year), builder.equal(mpsRoot.get("productionType"), type)));

        return new ListDataModel(em.createQuery(cq).getResultList());
    }

    public ArrayList<MasterProductionScheduleMonthlyEntity> getAllMPS() {
        Query query = em.createQuery("SELECT m FROM MasterProductionScheduleMonthly m WHERE m.archived!= 'true'");
        ArrayList<MasterProductionScheduleMonthlyEntity> mps = new ArrayList<MasterProductionScheduleMonthlyEntity>();
        for (Object o : query.getResultList()) {
            MasterProductionScheduleMonthlyEntity m = (MasterProductionScheduleMonthlyEntity) o;
            mps.add(m);
        }
        return mps;
    }

    public DataModel displayMPSByPeriod() {
        ArrayList<MasterProductionScheduleMonthlyEntity> mps = new ArrayList<MasterProductionScheduleMonthlyEntity>();
        for (int i = 0; i < getAllMPS().size(); i++) {
            boolean same = false;
            for (int j = 0; j < mps.size(); j++) {
                if (getAllMPS().get(i).getType().equals(mps.get(j).getType()) && getAllMPS().get(i).getYearOfProduction() == mps.get(j).getYearOfProduction()) {
                    same = true;
                    break;
                }
            }
            if (!same) {
                mps.add(getAllMPS().get(i));
            }
        }
        return new ListDataModel(mps);
    }

    public long getPreviousMonthOnHand(MasterProductionScheduleMonthlyEntity mps) {
        int year = mps.getYearOfProduction();
        Month month = mps.getMonthOfProduction();

        if (mps.getMonthOfProduction().equals(Month.JANUARY)) {
            month = Month.DECEMBER;
            year = year - 1;
        } else {
            month = Month.fromMonthNum(month.getMonthNum()-1);
        }
        System.out.println("month=" + month + "year=" + year);
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<MasterProductionScheduleMonthlyEntity> mpsRoot = cq.from(MasterProductionScheduleMonthlyEntity.class);
        cq.select(mpsRoot);
        cq.where(builder.and (builder.equal(mpsRoot.get("archived"), false), builder.equal(mpsRoot.get("monthOfProduction"), month), builder.equal(mpsRoot.get("yearOfProduction"), year)));

        MasterProductionScheduleMonthlyEntity m = (MasterProductionScheduleMonthlyEntity) em.createQuery(cq).getResultList().get(0);
        System.out.println("onhand=" + m.getSafetyStock());
        return m.getSafetyStock();
    }

    public ArrayList<MasterProductionScheduleMonthlyEntity> getAllMonthlyMPSFromMonthOnwards(MasterProductionScheduleMonthlyEntity mps) {
        ArrayList<MasterProductionScheduleMonthlyEntity> monthlyMPS = new ArrayList<MasterProductionScheduleMonthlyEntity>();
        Date thisMonth = new Date(mps.getYearOfProduction() - 1900, mps.getMonthOfProduction().getMonthNum() - 1, 1);
        //get all monthlymps
        Query query = em.createQuery("SELECT m FROM MasterProductionScheduleMonthly m WHERE m.archived!= 'true'");
        for (Object o : query.getResultList()) {
            MasterProductionScheduleMonthlyEntity m = (MasterProductionScheduleMonthlyEntity) o;
            Date date = new Date(m.getYearOfProduction() - 1900, m.getMonthOfProduction().getMonthNum() - 1, 1);
            System.out.println(date);
            if (date.after(thisMonth)) {
                monthlyMPS.add(m);
            }
        }
        return monthlyMPS;
    }
}
